package com.erp.framework.util.mainmenu;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.erp.app.common.ProjectConstants;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

public class MenuObjectDAO implements com.erp.framework.SimpleConstants {

    public ArrayList getMenuObjectList(String companyCode, String userId, String moduleName, Connection conn) {

        ArrayList menuObjectList = new ArrayList();

        Statement stmt = null;
        ResultSet rs = null;

        try {
			String sysLang = System.getProperty(SESSION_SYSTEM_LANG);
			if(CommonUtil.isEmpty(sysLang)){
				sysLang = LANG_DEFAULT;
			}

            String query = "SELECT A.GROUP_LEVEL, nvl(B.VISIBLE_LEVEL,0) VISIBLE_LEVEL, "
                    + " nvl(B.ENABLE_LEVEL,0) ENABLE_LEVEL, C.GROUP_ID,"
                    + " C.MENU_ID, C.OBJECT_ID, NVL(D.OBJECT_WEB_LABEL, C.OBJECT_NAME) object_name, C.OBJECT_TYPE, C.OBJECT_ACTION"
                    + " FROM T_USER A, T_COMPANY_MENU_OBJECT B, T_MENU_OBJECT C, T_MENU_OBJECT_LABEL D" 
					+ " WHERE A.COMPANY_CODE = B.COMPANY_CODE"
                    + " AND A.ROLE_GROUP_ID = B.ROLE_GROUP_ID" 
					+ " AND B.COMPANY_CODE = C.COMPANY_CODE"
                    + " AND B.GROUP_ID = C.GROUP_ID" 
					+ " AND B.MENU_ID = C.MENU_ID" 
					+ " AND B.OBJECT_ID = C.OBJECT_ID"
					+ " AND C.OBJECT_ID=D.OBJECT_ID(+)"
					+ " AND C.GROUP_ID=D.GROUP_ID(+)"
					+ " AND C.MENU_ID=D.MENU_ID(+)"
					+ " AND C.COMPANY_CODE=D.COMPANY_CODE(+)"
                    + " AND A.COMPANY_CODE='" + companyCode + "'" 
					+ " AND A.USER_CODE='" + userId + "'"
					+ " AND B.MENU_ID='" + moduleName + "'" 
					+ " AND D.LANG_CODE(+) = '" + sysLang + "' "
					+ " and c.object_type = '" + DBMENU_OBJECT_TYPE_BUTTON + "'"
					+ " AND NVL(A.ACTIVATION, 0) = '" + ProjectConstants.GENERIC_ACTIVE + "'";

            Logger.sql(this, "getMenuObjectList(), sql=" + query);

            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);

            if (rs != null) {
                while (rs.next()) {
                    MenuObject menuObject = (MenuObject) getResultSetValues(rs);
                    menuObjectList.add(menuObject);
                }
            }
        } catch (Exception e) {
            Logger.error(this, "getMenuObjectList(), error: " + e.getMessage());
            if (Logger.isTraceLevel()) {
                e.printStackTrace();
            }
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (stmt != null)
                    stmt.close();
            } catch (Exception e) {
                Logger.error(this, "getMenuObjectList(), error: " + e.getMessage());
                if (Logger.isTraceLevel()) {
                    e.printStackTrace();
                }
            }
        }
        return menuObjectList;
    }

    protected Object getResultSetValues(ResultSet rs) throws SQLException {

        MenuObject menuObject = new MenuObject();

        menuObject.setObjectId(rs.getString("OBJECT_ID"));
        menuObject.setMenuId(rs.getString("MENU_ID"));
        menuObject.setName(rs.getString("OBJECT_NAME"));
        menuObject.setGroupId(rs.getString("GROUP_ID"));
        menuObject.setObjectType(rs.getString("OBJECT_TYPE"));
        menuObject.setVisibleLevel(rs.getString("VISIBLE_LEVEL"));
        menuObject.setEnableLevel(rs.getString("ENABLE_LEVEL"));
        menuObject.setObjectAction(rs.getString("OBJECT_ACTION"));

        if (CommonUtil.strToInteger(rs.getString("GROUP_LEVEL")).intValue() <= CommonUtil.strToInteger(
                rs.getString("VISIBLE_LEVEL")).intValue())
            menuObject.setVisibleDisplay(ProjectConstants.GENERIC_YES);
        else
            menuObject.setVisibleDisplay(ProjectConstants.GENERIC_NO);

        if (CommonUtil.strToInteger(rs.getString("GROUP_LEVEL")).intValue() <= CommonUtil.strToInteger(
                rs.getString("ENABLE_LEVEL")).intValue())
            menuObject.setEnableDisplay(ProjectConstants.GENERIC_YES);
        else
            menuObject.setEnableDisplay(ProjectConstants.GENERIC_NO);

        return menuObject;
    }
}